<template>
  <div class="queue-container">
    <!-- 背景装饰元素 -->
    <div class="bg-decoration"></div>
    <div class="bg-particles"></div>
    
    <div class="header">
      <h1>
        <span class="text-glow">游戏候场中心</span>
      </h1>
      <div class="counter">
        <div class="number" :class="{ 'full': currentSize >= 30 }">{{ currentSize }}</div>
        <div class="label">/ 30</div>
      </div>
      <button 
        class="join-btn" 
        @click="joinQueue"
        :disabled="isInQueue || currentSize >= 30"
        :class="{ 'full': currentSize >= 30 }"
      >
        <span v-if="currentSize >= 30">人数已满</span>
        <span v-else>{{ isInQueue ? '排队中...' : '加入排队' }}</span>
      </button>
    </div>

    <div class="cinema-view">
      <div class="screen">
        <div class="screen-content">
          {{ currentSize >= 30 ? '游戏即将开始' : `等待玩家加入 (${currentSize}/30)` }}
        </div>
      </div>
      <div class="seats-grid">
        <div 
          v-for="(seat, index) in seats" 
          :key="index"
          class="seat"
          :class="{
            'occupied': seat.occupied,
            'selected': seat.selected,
            'highlight': index < currentSize && seat.selected,
            'new': index === currentSize - 1 && currentSize > 0 && !seat.selected
          }"
          @click="selectSeat(index)"
        >
          <span>{{ index + 1 }}</span>
        </div>
      </div>
    </div>

    <div v-if="gameStarting" class="game-start-modal">
      <div class="modal-content">
        <div class="modal-glow"></div>
        <h2>游戏即将开始!</h2>
        <p>玩家数量已满30人，游戏将在{{ countdown }}秒后开始...</p>
        <div class="countdown">{{ countdown }}</div>
      </div>
    </div>
  </div>
</template>

<script>
import SockJS from 'sockjs-client';
import Stomp from 'webstomp-client';

export default {
  data() {
    return {
      currentSize: 0,
      isInQueue: false,
      userId: '',
      seats: Array(30).fill().map(() => ({ 
        occupied: false, 
        selected: false 
      })),
      gameStarting: false,
      countdown: 5
    };
  },
  created() {
    this.userId = this.generateUserId();
    this.connectWebSocket();
    this.fetchQueueSize();
    this.createParticles();
  },
  methods: {
    generateUserId() {
      return  Math.random().toString(36).substring(2, 10);
    },
    connectWebSocket() {
      const socket = new SockJS('http://localhost:8000/queue-websocket');
      this.stompClient = Stomp.over(socket);
      
      this.stompClient.connect({}, frame => {
        console.log('Connected: ' + frame);
        
        // 订阅排队人数更新
        this.stompClient.subscribe('/topic/queueSize', response => {
          const newSize = JSON.parse(response.body);
          // 添加人数变化动画
          if (newSize > this.currentSize) {
            this.seats[newSize - 1].occupied = true;
          }
          this.currentSize = newSize;
          this.updateSeats();
        });
        
        // 订阅游戏开始通知
        this.stompClient.subscribe('/topic/gameStart', response => {
          const players = JSON.parse(response.body);
          this.startGameCountdown(players);
        });
      });
    },
    async fetchQueueSize() {
      try {
        const response = await this.$http.get('/api/queue/size');
        this.currentSize = response.size;
        this.updateSeats();
      } catch (error) {
        console.error('获取排队人数失败:', error);
      }
    },
    async joinQueue() {
      if (this.isInQueue || this.currentSize >= 30) return;
      
      try {
        this.isInQueue = true;
        const response = await this.$http.get(`/api/queue/join?userId=${this.userId}`);
        
        this.currentSize = response.data.size;
        this.updateSeats();
      } catch (error) {
        console.error('加入排队失败:', error);
        this.isInQueue = false;
      }
    },
    updateSeats() {
      this.seats = this.seats.map((seat, index) => ({
        ...seat,
        occupied: index < this.currentSize
      }));
    },
    selectSeat(index) {
      if (index < this.currentSize) {
        this.seats[index].selected = !this.seats[index].selected;
      }
    },
    startGameCountdown(players) {
      this.gameStarting = true;
      
      const timer = setInterval(() => {
        this.countdown--;
        
        if (this.countdown <= 0) {
          clearInterval(timer);
          this.resetQueue();
        }
      }, 1000);
    },
    resetQueue() {
      this.gameStarting = false;
      this.countdown = 5;
      this.isInQueue = false;
      this.currentSize = 0;
      this.seats = this.seats.map(seat => ({
        ...seat,
        occupied: false,
        selected: false
      }));
    },
    // 创建背景粒子效果
    createParticles() {
      const particles = document.querySelector('.bg-particles');
      if (!particles) return;
      
      for (let i = 0; i < 50; i++) {
        const particle = document.createElement('div');
        particle.className = 'particle';
        particle.style.left = `${Math.random() * 100}%`;
        particle.style.top = `${Math.random() * 100}%`;
        particle.style.width = `${Math.random() * 4 + 1}px`;
        particle.style.height = particle.style.width;
        particle.style.animationDuration = `${Math.random() * 20 + 10}s`;
        particle.style.animationDelay = `${Math.random() * 10}s`;
        particles.appendChild(particle);
      }
    }
  }
};
</script>

<style scoped>
.queue-container {
  max-width: 1000px;
  margin: 0 auto;
  padding: 20px;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  position: relative;
  min-height: 100vh;
  z-index: 1;
  overflow: hidden;
}

/* 背景效果 */
.bg-decoration {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
  z-index: -2;
}

.bg-particles {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: -1;
}

.particle {
  position: absolute;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 50%;
  animation: float 30s infinite linear;
}

@keyframes float {
  0% {
    transform: translate(0, 0) rotate(0deg);
    opacity: 0.1;
  }
  25% {
    opacity: 0.3;
  }
  50% {
    opacity: 0.1;
  }
  75% {
    opacity: 0.3;
  }
  100% {
    transform: translate(calc(-100vw + 100%), calc(-100vh + 100%)) rotate(360deg);
    opacity: 0.1;
  }
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 30px;
  padding: 25px;
  background: linear-gradient(135deg, #6d28d9 0%, #4f46e5 100%);
  border-radius: 16px;
  color: white;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.3);
  position: relative;
  overflow: hidden;
}

.header::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, rgba(255,255,255,0) 70%);
  animation: rotate 20s infinite linear;
}

@keyframes rotate {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

h1 {
  margin: 0;
  font-size: 2rem;
  position: relative;
  z-index: 1;
}

.text-glow {
  text-shadow: 0 0 10px rgba(255, 255, 255, 0.7), 
               0 0 20px rgba(255, 255, 255, 0.5);
}

.counter {
  display: flex;
  align-items: baseline;
  font-size: 1.8rem;
  font-weight: bold;
  position: relative;
  z-index: 1;
}

.number {
  font-size: 4rem;
  font-weight: bold;
  margin: 0 10px;
  transition: all 0.5s ease;
  text-shadow: 0 0 10px rgba(255, 255, 255, 0.3);
}

.number.full {
  color: #f97316;
  animation: pulse 1s infinite alternate;
  text-shadow: 0 0 15px rgba(249, 115, 22, 0.7);
}

@keyframes pulse {
  from { transform: scale(1); }
  to { transform: scale(1.05); }
}

.join-btn {
  background: #22c55e;
  color: white;
  border: none;
  padding: 14px 35px;
  font-size: 1.2rem;
  border-radius: 50px;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(34, 197, 94, 0.4);
  font-weight: bold;
  position: relative;
  z-index: 1;
  overflow: hidden;
}

.join-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
  transition: 0.5s;
}

.join-btn:hover:not(:disabled)::before {
  left: 100%;
}

.join-btn:hover:not(:disabled) {
  background: #16a34a;
  transform: translateY(-3px);
  box-shadow: 0 6px 20px rgba(34, 197, 94, 0.6);
}

.join-btn:disabled {
  background: #94a3b8;
  cursor: not-allowed;
  box-shadow: none;
}

.join-btn.full {
  background: #f97316;
  box-shadow: 0 4px 15px rgba(249, 115, 22, 0.4);
}

.join-btn.full:hover:not(:disabled) {
  background: #ea580c;
  box-shadow: 0 6px 20px rgba(249, 115, 22, 0.6);
}

.cinema-view {
  background: #0f172a;
  border-radius: 16px;
  padding: 30px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.4);
  position: relative;
  overflow: hidden;
}

.cinema-view::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: radial-gradient(circle at top center, rgba(148, 163, 184, 0.05) 0%, transparent 70%);
  pointer-events: none;
}

.screen {
  background: linear-gradient(to bottom, #3b82f6, #60a5fa);
  height: 60px;
  margin-bottom: 40px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: bold;
  letter-spacing: 2px;
  box-shadow: 0 10px 30px rgba(59, 130, 246, 0.4);
  position: relative;
  overflow: hidden;
}

.screen::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 50%;
  background: linear-gradient(to bottom, rgba(255,255,255,0.3), transparent);
}

.screen-content {
  position: relative;
  z-index: 1;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
  transition: all 0.5s ease;
}

.seats-grid {
  display: grid;
  grid-template-columns: repeat(10, 1fr);
  gap: 15px;
  perspective: 1000px;
}

.seat {
  aspect-ratio: 1;
  background: #1e293b;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s ease;
  color: #94a3b8;
  font-size: 0.9rem;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3), 
              inset 0 2px 2px rgba(255, 255, 255, 0.05);
  transform-style: preserve-3d;
  position: relative;
}

.seat::before {
  content: '';
  position: absolute;
  top: 5px;
  left: 5px;
  right: 5px;
  bottom: 5px;
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.03);
  transform: translateZ(2px);
}

.seat:hover:not(.occupied) {
  transform: translateY(-5px) rotateX(5deg);
  box-shadow: 0 10px 15px rgba(0, 0, 0, 0.4);
}

.seat.occupied {
  background: #3b82f6;
  color: white;
  box-shadow: 0 4px 15px rgba(59, 130, 246, 0.3);
  animation: seatFadeIn 0.5s ease-out;
}

@keyframes seatFadeIn {
  0% {
    transform: scale(0.8) rotateX(20deg);
    opacity: 0.5;
  }
  100% {
    transform: scale(1) rotateX(0);
    opacity: 1;
  }
}

.seat.selected {
  background: #f59e0b;
  color: white;
  box-shadow: 0 6px 20px rgba(245, 158, 11, 0.5);
  transform: scale(1.05);
}

.seat.highlight {
  animation: seatPulse 1.5s infinite;
}

@keyframes seatPulse {
  0% {
    box-shadow: 0 6px 20px rgba(245, 158, 11, 0.5);
  }
  50% {
    box-shadow: 0 6px 30px rgba(245, 158, 11, 0.8);
  }
  100% {
    box-shadow: 0 6px 20px rgba(245, 158, 11, 0.5);
  }
}

.seat.new {
  animation: seatNew 1.5s ease-out;
}

@keyframes seatNew {
  0% {
    background: #ec4899;
    box-shadow: 0 6px 30px rgba(236, 72, 153, 0.8);
    transform: scale(1.1);
  }
  50% {
    background: #ec4899;
    box-shadow: 0 6px 30px rgba(236, 72, 153, 0.8);
  }
  100% {
    background: #3b82f6;
    box-shadow: 0 4px 15px rgba(59, 130, 246, 0.3);
    transform: scale(1);
  }
}

.seat span {
  position: relative;
  z-index: 1;
}

.game-start-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.9);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  opacity: 0;
  animation: fadeIn 0.6s forwards;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.modal-content {
  background: #1e293b;
  padding: 50px;
  border-radius: 20px;
  text-align: center;
  max-width: 500px;
  width: 90%;
  box-shadow: 0 20px 80px rgba(0, 0, 0, 0.6);
  animation: zoomIn 0.6s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  position: relative;
  overflow: hidden;
  border: 1px solid rgba(59, 130, 246, 0.3);
}

.modal-glow {
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(59, 130, 246, 0.3) 0%, transparent 70%);
  animation: rotate 15s infinite linear;
}

@keyframes zoomIn {
  from { 
    transform: scale(0.7); 
    opacity: 0;
  }
  to { 
    transform: scale(1); 
    opacity: 1;
  }
}

.modal-content h2 {
  color: #3b82f6;
  font-size: 2.8rem;
  margin-bottom: 20px;
  position: relative;
  z-index: 1;
  text-shadow: 0 0 20px rgba(59, 130, 246, 0.5);
}

.modal-content p {
  font-size: 1.3rem;
  margin-bottom: 30px;
  color: #e2e8f0;
  position: relative;
  z-index: 1;
}

.countdown {
  font-size: 5rem;
  font-weight: bold;
  color: #f97316;
  animation: bounce 1s infinite alternate;
  position: relative;
  z-index: 1;
  text-shadow: 0 0 20px rgba(249, 115, 22, 0.5);
}

@keyframes bounce {
  from { transform: scale(1); }
  to { transform: scale(1.1); }
}
</style>
